package com.coment.game.fight;

import com.coment.cfg.luban.MonsterConfig;
import com.coment.cfg.luban.Tables;
import com.coment.cfg.luban.items.EquipCfg;
import com.coment.cfg.luban.roles.CharacterBaseAttributeConfig;
import com.coment.cfg.luban.roles.NumericalConfig;
import com.coment.game.core.AttributeFieldMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@AllArgsConstructor
public class AttributeManagerLogic {
    /// 技能效果类型 - 非一级属性
    private static final List<Integer> EXCLUDED_EFFECT_TYPES = List.of(9, 10);
    /// 属性字段映射
    public static AttributeFieldMapper attributeFieldMapper;
    /// 属性常量
    public static Map<String, Integer> constantsMap;
    /// 属性值上限
    public static final Map<Integer, Double> ATTR_MAX_VALUE = new ConcurrentHashMap<>();
    /// 一级属性的映射信息
    public static Map<Integer, Integer[]> attrsCfg = new ConcurrentHashMap<>();
    /// 实体属性
    public static final ConcurrentHashMap<Integer, Attrs> inventoryAttrs = new ConcurrentHashMap<>();
    public static final ConcurrentHashMap<Integer, Attrs> monsterAttrs = new ConcurrentHashMap<>();

    /// k:lv v:等级属性
    public static final ConcurrentHashMap<Integer, Attrs> ROLES_ATTRS_LV = new ConcurrentHashMap<>();
    public static final ConcurrentHashMap<Integer, Attrs> MONSTER_ATTRS = new ConcurrentHashMap<>();
    /// 角色天赋属性
    public static final ConcurrentHashMap<Integer, Attrs> ROLES_TC = new ConcurrentHashMap<>();
    /// 先天气运 k1:cardId k2:lv v:value
    public static final ConcurrentHashMap<Integer, Map<Integer, Attrs>> CARD_XTQY_CFG = new ConcurrentHashMap<>();
    /// 4.羁绊
    public static final ConcurrentHashMap<Integer, Map<Integer, Attrs>> CARD_JB_CFG = new ConcurrentHashMap<>();
    /// 获取全局属性处理器
    public static Function<Integer, Attrs> obtainAttrsGHandler;

    public static final ConcurrentHashMap<Integer,Integer> ATTRS_NUMBER_TYPE = new ConcurrentHashMap<>();


    static {
        loadingCfg();
    }

    public static void loadingCfg() {
        attributeFieldMapper = new AttributeFieldMapper(Tables.cfg.get_tbnumericalconfig().getDataMap());

        constantsMap = attributeFieldMapper.getFieldNameToIdMap();

        ArrayList<NumericalConfig> numericalConfigs = Tables.cfg.get_tbnumericalconfig().getDataList();

        for (NumericalConfig numericalConfig : numericalConfigs) {
            ATTR_MAX_VALUE.put(numericalConfig.NumericalID, numericalConfig.maxValue);
            ATTRS_NUMBER_TYPE.put(numericalConfig.NumericalID, numericalConfig.NumType);
        }

        Map<Integer, List<NumericalConfig>> collect = numericalConfigs
                .stream()
                .filter(cf -> !EXCLUDED_EFFECT_TYPES.contains(cf.effectType))
                .collect(Collectors.groupingBy(config -> constantsMap.get(config.effectAttr)));
        collect.forEach((key, value) -> {
            Integer[] integers = new Integer[20];
            for (NumericalConfig numericalConfig : value) {
                integers[numericalConfig.effectType] = numericalConfig.NumericalID;
            }
            attrsCfg.put(key, integers);
        });

        // 物品属性
        for (EquipCfg equipCfg : Tables.cfg.get_tbequipcfg().getDataList()) {
            Attrs EQUIP_ATTRS = AttributeManagerLogic.obtainCfgAttrs(equipCfg);
            inventoryAttrs.put(equipCfg.id, EQUIP_ATTRS);
        }
        // 加载怪物属性
        for (MonsterConfig cfg : Tables.cfg.get_tbmonsterconfig().getDataList()) {
            MONSTER_ATTRS.put(cfg.ID, AttributeManagerLogic.obtainCfgAttrs(cfg));
        }

        // 等级属性
        for (CharacterBaseAttributeConfig baseAttributeConfig : Tables.cfg.get_tbcharacterbaseattributeconfig().getDataList()) {
            ROLES_ATTRS_LV.put(baseAttributeConfig.level, obtainCfgAttrs(baseAttributeConfig));
        }

        // 天赋
        Tables.cfg.get_tbcardtalentconfig().getDataMap().forEach((cardId, ctCfg) -> {
            Attrs tcPanel = AttributeManagerLogic.obtainCfgAttrs(ctCfg);
            ROLES_TC.put(cardId, tcPanel);
        });

        // 先天气运
        Tables.cfg.get_tbcardxtqqcfg().getDataList().forEach((cfg) -> {
            Map<Integer, Attrs> orDefault = CARD_XTQY_CFG.getOrDefault(cfg.cardId, new HashMap<>());
            Attrs value = new Attrs(cfg.value);
            orDefault.put(cfg.lv, value);
            CARD_XTQY_CFG.put(cfg.cardId, orDefault);
        });

        // 羁绊
        Tables.cfg.get_tbcardcamaraderiecfg().getDataList().forEach((cfg) -> {
            Map<Integer, Attrs> orDefault = CARD_JB_CFG.getOrDefault(cfg.cardId, new HashMap<>());
            Attrs value = obtainCfgAttrs(cfg);
            orDefault.put(cfg.lv, value);
            CARD_JB_CFG.put(cfg.cardId, orDefault);
        });
    }


    /// 提取配置属性
    public static Attrs obtainCfgAttrs(
            luban.Cfg cfg
    ) {
        Attrs map = attributeFieldMapper.obtainAttrs(cfg);
        map.forEach((id, cfgValue) ->
                map.merge(id, Attrs.NUMBER_TYPE.get(id).doubleValue(), (value, typeValue) -> value / typeValue)
        );
        return map;
    }

    /// 提取对象属性
    public static Attrs obtainAttrs(
            Object cfg
    ) {
        return attributeFieldMapper.obtainAttrs(cfg);
    }

}
